package wjh.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import wjh.pojo.Doctor;
import wjh.service.wyDepartmentService;
import wjh.service.wyDoctorService;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/doctorList")
public class wyDoctorController extends HttpServlet {
    @Autowired
    private wyDoctorService doctorService;
    @Qualifier("httpServletRequest")
    @Autowired
    private ServletRequest httpServletRequest;

    @Autowired
    private wyDepartmentService wyDepartmentService;

    @RequestMapping("/doctorFindAll")
    public String doctorFindAll(

            @RequestParam(required = false,defaultValue ="1")Integer pageNum,
            @RequestParam(required = false,defaultValue ="3")Integer pageSize,
            String likename, Model model
    ){
        PageHelper.startPage(pageNum,pageSize);
        List<Doctor> doctorList=doctorService.like(likename);
        PageInfo<Doctor> pageInfo = new PageInfo<>(doctorList);
        model.addAttribute("page",pageInfo);
        model.addAttribute("likename",likename);
        return "doctorList";
    }
    @RequestMapping("/addDoctor")
    public String addDoctor(Doctor doctor, MultipartFile file,HttpServletRequest request) {
        int idByName = wyDepartmentService.findIdByName(doctor.getDepartment());
        doctor.setDepartment_id(idByName);
        doctorService.addDoctor(doctor,file,request);
        return "redirect:/doctorList/doctorFindAll.do";


    }

@RequestMapping("/doctorDel")
public ResponseEntity<String> deleteDoctorByid(@RequestParam String ids) {
    try {
        doctorService.deleteDoctorByid(ids);
        return ResponseEntity.ok("删除成功");
    } catch (Exception e) {
        // 记录异常并返回错误信息
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("删除失败");
    }
}


    @RequestMapping("/findDoctorById")
    public String findDoctorById(@RequestParam("id") String id, Model model) {
        Doctor doctor=doctorService.findDoctorById (Integer.parseInt(id));
        model.addAttribute("doctor",doctor);
        return "updateDoctor";
    }

    @RequestMapping("/updateDoctor")
    public String updateDoctor(Doctor doctor,Model model,MultipartFile file,HttpServletRequest request, String phname)  {
        int department_id= wyDepartmentService.findIdByName(doctor.getDepartment());
        doctor.setDepartment_id(department_id);
        doctorService.updateDoctor(doctor,file,request, phname);
        int id = doctor.getId();
        model.addAttribute("id", id);
        model.addAttribute("Doctor", doctor);
        return "redirect:/doctorList/doctorFindAll.do";
    }
}
